const app = getApp()
let qCloud = require("../../../index.js");
let timerInterval = null;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgPath: qCloud.config.STATIC_IMAGE_PATH,
        tabIndex: 1,
        codeDisabled: false,
        codeText: "获取验证码",
        countDown: 60,
        maxTime: 60,

        organName: "",
        phone: "",
        realName: "",
        code: "",
        password: "",
        confirmPassword: "",
        modifyPhone: "",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let userInfo = qCloud.session.getUser();
        this.setData({
            organName: userInfo.organName,
            phone: qCloud.crypto.decrypt(userInfo.phoneEncrypt),
            realName: qCloud.crypto.decrypt(userInfo.realNameEncrypt)
        })
    },


    bindSendCodeClick: function (e) {
        let self = this;
        if (this.data.tabIndex == 3) {
            if (this.data.modifyPhone == "") {
                wx.showToast({
                    title: "请输入新手机号",
                    icon: "error"
                });
                return;
            }
        }
        let params = {
            type: this.data.tabIndex == 1 ? "2" : "1",
            phone: this.data.tabIndex == 2 ? qCloud.session.getUser().phoneEncrypt : qCloud.crypto.encrypt(self.data.modifyPhone)
        };

        // 发送验证码
        app.fetchNoTokenApis(self, qCloud.api.smsCode, params, "POST", function (e) {
            if (e.data.code != 200) {
                if (timerInterval) {
                    clearTimeout(timerInterval);
                }
                wx.showModal({
                    content: e.data.msg
                });
            } else {
                timerInterval = setTimeout(self.refreshCode, 1000);
            }
        })
    },

    refreshCode: function () {
        let self = this;
        if (self.data.countDown > 0) {
            self.setData({
                codeDisabled: true,
                countDown: self.data.countDown - 1,
                codeText: self.data.countDown + "s后重试"
            })
            timerInterval = setTimeout(self.refreshCode, 1000);
        } else {
            self.setData({
                codeDisabled: false,
                codeText: "重新获取",
                countDown: self.data.maxTime,
            })
            if (timerInterval) {
                clearTimeout(timerInterval);
            }
        }
    },

    codeInput: function (e) {
        this.setData({
            code: e.detail.value
        })
    },

    passwordInput: function (e) {
        this.setData({
            password: e.detail.value
        })
    },

    confirmPassword: function (e) {
        this.setData({
            confirmPassword: e.detail.value
        })
    },

    phoneInput: function (e) {
        this.setData({
            modifyPhone: e.detail.value
        })
    },

    // 顶部菜单选择
    tabClick: function (e) {
        let userInfo = qCloud.session.getUser();
        this.setData({
            tabIndex: e.currentTarget.dataset.index,
            phone: qCloud.crypto.decrypt(userInfo.phoneEncrypt),
            realName: qCloud.crypto.decrypt(userInfo.realNameEncrypt)
        });
    },

    realNameInput: function (e) {
        this.setData({
            realName: e.detail.value
        })
    },

    submitUpdateUser: function () {
        let self = this;
        if (this.data.tabIndex == 1) {
            if (this.data.realName == "") {
                wx.showToast({
                    title: "真实姓名不能为空",
                    icon: "error"
                });
                return;
            }

            let params = {
                realName: self.data.realName
            }

            app.fetchApis(self, qCloud.api.updateUser, params, "POST", function (e) {
                let result = e.data;
                if (result.code == 200) {
                    wx.showToast({
                        title: "修改成功",
                        icon: "success"
                    });
                    let userInfo = qCloud.session.getUser();
                    userInfo.realNameEncrypt = qCloud.crypto.encrypt(self.data.realName);
                    qCloud.session.setUser(userInfo);
                } else {
                    wx.showToast({
                        title: result.msg || "修改失败",
                        icon: "error"
                    });
                }
            });
        }

        if (this.data.tabIndex == 2) {
            if (this.data.code == "") {
                wx.showToast({
                    title: "验证码不能为空",
                    icon: "error"
                });
                return;
            }

            if (this.data.password == "") {
                wx.showToast({
                    title: "新密码不能为空",
                    icon: "error"
                });
                return;
            }

            if (this.data.confirmPassword == "") {
                wx.showToast({
                    title: "确认密码不能为空",
                    icon: "error"
                });
                return;
            }

            if (this.data.confirmPassword != this.data.password) {
                wx.showToast({
                    title: "两次密码输入不一致",
                    icon: "error"
                });
                return;
            }

            let params = {
                phone: qCloud.session.getUser().phoneEncrypt,
                code: self.data.code,
                password: qCloud.crypto.encrypt(self.data.confirmPassword)
            }

            app.fetchApis(self, qCloud.api.updatePassword, params, "POST", function (e) {
                let result = e.data;
                if (result.code == 200) {
                    wx.showToast({
                        title: "修改成功",
                        icon: "success"
                    })
                } else {
                    wx.showToast({
                        title: result.msg || "修改失败",
                        icon: "error"
                    })
                }
            });
        }

        if (this.data.tabIndex == 3) {
            if (this.data.modifyPhone == "") {
                wx.showToast({
                    title: "请输入新手机号",
                    icon: "error"
                });
                return;
            }

            let params = {
                phone: qCloud.crypto.encrypt(self.data.modifyPhone),
                code: self.data.code,
            }

            app.fetchApis(self, qCloud.api.updatePhone, params, "POST", function (e) {
                let result = e.data;
                if (result.code == 200) {
                    wx.showToast({
                        title: "修改成功",
                        icon: "success"
                    });
                    let userInfo = qCloud.session.getUser();
                    userInfo.phoneEncrypt = qCloud.crypto.encrypt(self.data.modifyPhone);
                    qCloud.session.setUser(userInfo);
                } else {
                    wx.showToast({
                        title: result.msg || "修改失败",
                        icon: "error"
                    })
                }
            });
        }
    },


})
